﻿using Code;
using github.heerheer.card2.UI.ViewModels;
using github.heerheer.card2.UI.Views.Dialog;
using HeerCards.Info;
using Panuon.UI.Silver;
using System.IO;
using System.Windows;
using System.Windows.Controls;

namespace github.heerheer.card2.UI.Views
{
    /// <summary>
    /// SettingsView.xaml 的交互逻辑
    /// </summary>
    public partial class SettingsView : UserControl
    {
        private SettingsViewModel SettingsViewModel { get; set; }

        public SettingsView()
        {
            InitializeComponent();
            this.DataContext = SettingsViewModel = new SettingsViewModel();
            SettingsViewModel.LoadConfig(App.getGlobalsettingPath());
        }

        private void Btn_EnableApp(object sender, RoutedEventArgs e)
        {
            SettingsViewModel.GlobalSetting.enableApp = !SettingsViewModel.GlobalSetting.enableApp;
        }

        private void Btn_EnableImage(object sender, RoutedEventArgs e)
        {
            SettingsViewModel.GlobalSetting.useImage = !SettingsViewModel.GlobalSetting.useImage;
        }

        private void Btn_EnableSaveData(object sender, RoutedEventArgs e)
        {
            SettingsViewModel.GlobalSetting.saveData = !SettingsViewModel.GlobalSetting.saveData;
        }

        private void Btn_UseTimesLimit(object sender, RoutedEventArgs e)
        {
            SettingsViewModel.GlobalSetting.UseTimesLimit = !SettingsViewModel.GlobalSetting.UseTimesLimit;
        }

        private void Btn_UseTimesLimitChecked(object sender, RoutedEventArgs e)
        {
            //SettingsViewModel.GlobalSetting.UseTimesLimit = !SettingsViewModel.GlobalSetting.UseTimesLimit;
            if (!SettingsViewModel.GlobalSetting.saveData)
            {
                MessageBoxX.Show("使用本功能需要开启数据储存", "注意");
            }
        }

        private void Btn_AddGroup(object sender, RoutedEventArgs e)
        {
            //Add
            InputDialog inp = new InputDialog("群号");
            if (inp.ShowDialog() == true)
            {
                this.SettingsViewModel.GlobalSetting.allowGroups += inp.Value + ";";
            }
            //SettingsViewModel.GlobalSetting.allowGroups +=;
        }

        private void Btn_DeleteGroup(object sender, RoutedEventArgs e)
        {
            if (allowGroupsListbox.SelectedIndex >= 0)
            {
                this.SettingsViewModel.GlobalSetting.allowGroups =
                    this.SettingsViewModel.GlobalSetting.allowGroups.Replace(allowGroupsListbox.SelectedItem.ToString() + ";", "");
            }
        }

        //保存代码
        private void Btn_SaveGlobalSettings(object sender, RoutedEventArgs e)
        {
            File.WriteAllText(AppMain.Common?.AppDirectory + "GobalSettings.json", LitJson.JsonMapper.ToJson(SettingsViewModel.GlobalSetting));
            MessageBoxX.Show("保存成功", "保存");
        }

        private void Btn_OpenVariableDialog(object sender, RoutedEventArgs e)
        {
            var d = new VariableDialog();
            d.Show();
        }

        private void Btn_AddCheckinReward(object sender, RoutedEventArgs e)
        {
            RewardInfo info = new RewardInfo();
            InputDialog inp = new InputDialog("货币名称");
            if (inp.ShowDialog() == true)
            {
                info.Name = inp.Value;
            }
            inp = new InputDialog("最小值");
            if (inp.ShowDialog() == true)
            {
                info.Min = int.Parse( inp.Value);
            }
            inp = new InputDialog("最大值");
            if (inp.ShowDialog() == true)
            {
                info.Max = int.Parse( inp.Value);
            }

            SettingsViewModel.GlobalSetting.CheckinRewards.Add(info);
            CRList.ItemsSource = null;
            CRList.ItemsSource = SettingsViewModel.GlobalSetting.CheckinRewards;
            ;
        }

        private void Btn_RemoveCheckinReward(object sender, RoutedEventArgs e)
        {
            SettingsViewModel.GlobalSetting.CheckinRewards.Remove((RewardInfo)CRList.SelectedItem);
            CRList.ItemsSource = null;
            CRList.ItemsSource = SettingsViewModel.GlobalSetting.CheckinRewards;
        }

        private void Btn_HowUseCommandHelper(object sender, RoutedEventArgs e)
        {
            MessageBoxX.Show("请仔细查看数据目录下Commands.txt内注释~","如何使用");
        }
    }
}